package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebAddressDao;
import ysq.xyg.web.model.po.WebAddress;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebAddressVo;
import ysq.xyg.web.service.WebAddressService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 业务模块-通用地址service实现
 *
 * @author congcong tao
 * @date 2019-5-14 14:23:39
 */
@Service("webAddressService")
public class WebAddressServiceImpl extends BaseServiceImpl<WebAddress> implements WebAddressService {

    @Resource
    private WebAddressDao webAddressDao;

    private WebAddress webAddress;

    @Autowired
    public void setBaseDao(WebAddressDao webAddressDao) {
        this.baseDao = webAddressDao;
    }

    /**
     * 分页查询
     *
     * @param webAddressVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-14 14:23:39
     */
    @Override
    public ReturnEntity page(WebAddressVo webAddressVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebAddress> list = acceptList(webAddressVo);
        PageInfo<WebAddress> page=new PageInfo<WebAddress>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webAddressVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-14 14:23:39
     */
    @Override
    public ReturnEntity addOrUpdate(WebAddressVo webAddressVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webAddressVo.getId())){
            webAddress=new WebAddress(currentUser);
            isUpate=false;
        }else{
            webAddress=selectByPrimaryKey(webAddressVo.getId());
            if(ObjectUtil.isNull(webAddress)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webAddress.setUpdateTime(new Date());
            webAddress.setUpdateUser(currentUser.getId());
        }
        webAddress.setProvince(webAddressVo.getProvince());
        webAddress.setCity(webAddressVo.getCity());
        webAddress.setArea(webAddressVo.getArea());
        webAddress.setDescripts(webAddressVo.getDescripts());
        webAddress.setType(webAddressVo.getType());
        webAddress.setOrderNum(webAddressVo.getOrderNum());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webAddress);
        }else{
            num=insert(webAddress);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-14 14:23:39
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebAddress webAddress=selectByPrimaryKey(id);
        if(webAddress!=null){
            webAddress.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webAddress.setUpdateTime(new Date());
            webAddress.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webAddress);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webAddressVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-5-14 14:23:39
     */
    @Override
    public List<WebAddress> acceptList(WebAddressVo webAddressVo){
        Example example = new Example(WebAddress.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webAddressVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webAddressVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webAddressDao.selectByExample(example);
    }
}
